#include <Array.au3>
#include <_NTServices.au3>
#include <log.au3>

; #INDEX# =======================================================================================================================
; Title .........: an interface to parse golbal configure file & gererate a result file
; AutoIt Version : 3.3
; Author ......: Allen Wang
; Description ...: This module contains various functions for providing configure value.
; ===============================================================================================================================

; #CURRENT# =====================================================================================================================
;generate_config_filename
;generate_configure_result($output_file)
;transfor_configure_path()
;get_reg_value($reg_section_name,$output_file)
;get_file_status($file_section_name,$output_file)
;get_folder_status($folder_section_name,$output_file)
;get_server_status($service_section_name,$output_file)
;transfor_parameter_to_path($parameter)
;get_globalvariable_path($parameter)
;get_action()
;set_action($var)
;get_falcon_build()
;set_falcon_build()
;get_disksafe_version()
;set_disksafe_version()
;get_disksafe_build()
;set_disksafe_build()
;get_ima_version()
;set_ima_version()
;get_ima_build()
;set_ima_build()
;get_configure_filename()
; ===============================================================================================================================

;===============
; API Constants
;===============
Const $globalvariable_file_name = @ScriptDir &"\config\GlobalSetting.ini"
Const $BuildInfo				= "Build Information"
Const $EnvVar					= "Environment Variable"
Const $ConfigPara				= "Config Parameter"
Const $ConfigPara_key			= "Config_File_Name"

Const $action_key 				= "Action"
Const $falcon_build 			= "Is_Falcon_Build"
Const $falcon_build_name 		= "\FalconStor"
Const $Disksafe_product_name 	= "\DiskSafe"
Const $ima_product_name 		= "\IMA"
Const $Disksafe_version 		= "DiskSafe_Version"
Const $Disksafe_build 			= "DiskSafe_Build"
Const $IMA_version 				= "IMA_Version"
Const $IMA_build 				= "IMA_Build"
Const $Default_Para_Setting		= "(default)"
Const $config_location			= @ScriptDir & "\config\ItemConfig"
Const $config_filename 			= generate_config_filename()	

Const $RegRootPath 				= "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Dsksafe\Parameters"
Const $success 					= "1"
Const $stopped					= "0"
Const $no_exist 				= "NA"
Const $dont_care 				= ";---"
Const $PROGRAMFILESDIR			= @ProgramFilesDir 	;c:\program files
Const $DOCUMENTDIR   		    = @AppDataCommonDir ;c:\Docnments and Settings\All Users\A[[lication Data
Const $SYSTEMDIR		        = @SystemDir        ;C;\WINDOWS\system32
;[Old];Global $glob_ini_index 	= IniReadSection($globalvariable_file_name, "Global Variables")
Global $glob_ini_index 			= IniReadSection($globalvariable_file_name, $BuildInfo)
Global $glob_ini_index_env_var 	= IniReadSection($globalvariable_file_name, $EnvVar)

; #FUNCTION# ====================================================================================================================
; Name...........: generate_config_filename
; Description ...:
; Parameters ....:
;
; Return values .: config filename 
;                  
; Author ........: Janice Wang

; ===============================================================================================================================

#cs
;test_S
$r = generate_config_filename()
MsgBox(0,"config name",$r)
$t = IniRead($r,"Service","Item1","not")
MsgBox(0,"dd",$t)
;test_E
#ce

Func generate_config_filename()
	
	$value = IniRead($globalvariable_file_name,$ConfigPara,$ConfigPara_key,"ConfigPara Not Found")
	
	If $value <> $Default_Para_Setting Then
		$value = $config_location & "\" & $value		
		Return $value		
	Else
				
		$DS_version = get_disksafe_version()		
		$value = $config_location & "\ItemConfig-" & $DS_version &".ini"
		Return $value	
	EndIf	
	
EndFunc


; #FUNCTION# ====================================================================================================================
; Name...........: generate_configure_result($output_file)
; Description ...:
; Parameters ....:$output_file - output result to this ini file
;
; Return values .: Success - Index of last added item
;                  Failure - -1, sets @error
; Author ........: Allen Wang

; ===============================================================================================================================

#cs
;test_S
$output_file = @ScriptDir & "\result\testRSTtt.ini"
generate_configure_result($output_file)
MsgBox(0,"","generate_configure_result Complete...")
;test_E
#ce

Func generate_configure_result($output_file)

	_log("generate_configure_result() IN")

	transfor_configure_path()
	
	get_server_status("Service",$output_file)
	
	get_folder_status("Folder",$output_file)
	
	get_file_status("File",$output_file)
	
	get_reg_value("Registry",$output_file)
	
	_log("generate_configure_result() OUT")

EndFunc ;generate_configure_result

; #FUNCTION# ====================================================================================================================
; Name...........: transfor_configure_path()
; Description ...: read golbalvariable.ini to physical path
; Parameters ....:
;
; Return values .: Success - Index of last added item
;                  Failure - -1, sets @error
; Author ........: Allen Wang

; ===============================================================================================================================



Func transfor_configure_path()

	_log("transfor_configure_path() IN")
	;change falconstor build to phrase "\Falconstor" or "" depend on "Isafonstor"
	local $temp=get_falcon_build(),$temp1

	if $temp = 1 Then
		$temp1=_ArraySearch($glob_ini_index, $falcon_build, 0, 0, 1, 0)

		;_log("transfor_configure_path() MID1-1, $temp1 = " & $temp1)

		;_ArrayDisplay($glob_ini_index, "transfor_configure_path() Show ary env var...")	;test

		$glob_ini_index[$temp1][1] = $falcon_build_name

	Else
		$temp1=_ArraySearch($glob_ini_index, $falcon_build, 0, 0, 1, 0)
		;_log("transfor_configure_path() MID1-2, $temp1 = " & $temp1)
		;MsgBox(0,"$temp1= ",$temp1)

		$glob_ini_index[$temp1][1]=""

	EndIf

	;_ArrayDisplay($glob_ini_index, "transfor_configure_path() $Array-transfor_configure_path-1")	;test

	_log("transfor_configure_path() MID")


	for $z=1 To $glob_ini_index_env_var[0][0]

		transfor_parameter_to_path($glob_ini_index_env_var[$z][0])
		;Local $temp = get_globalvariable_path($glob_ini_index[$z][0])
		;MsgBox(0,"index ",$temp)
	Next
	;_ArrayDisplay($glob_ini_index_env_var, "Result: $Array-transfor_configure_path-2")	;test

	_log("transfor_configure_path() OUT")

EndFunc


; #FUNCTION# ====================================================================================================================
; Name...........: get_reg_value($reg_section_name,$output_file)
; Description ...:
; Parameters ....: $reg_section_name - resction name to read reg
;                  $output_file - output result to this ini file
; Return values .: Success - Index of last added item
;                  Failure - -1, sets @error
; Author ........: Allen Wang

; ===============================================================================================================================
Func get_reg_value($reg_section_name,$output_file)


	_log("get_reg_value() IN")


	 Local $temp,$path,$array,$value,$var1,$i=1
	 Local $var
	 ; read all section & key valueform configure file
	 Local $config_ini_index = IniReadSection($config_filename, $reg_section_name)
		;_ArrayDisplay($config_ini_index, "$Array")

	for $temp = 1 To $config_ini_index[0][0]
			$array = StringSplit($config_ini_index[$temp][1],";")
			$array[1] = StringReplace($array[1], "$", "") ;delete "$"

			;_log("get_reg_value() MID")

			If Not 0 = StringInStr($array[1], "*") Then
				;MsgBox(0,"folder name ",$array[1])
				$array[1] = StringReplace($array[1], "*", "") ;delete "*"
				$path = get_globalvariable_path($array[1])
				;MsgBox(0,"path",$path)

				For $i = 1 to 20 ;$i must begin form 1
					$var = RegEnumKey($path, $i)
					if @error <> 0 Then ExitLoop
					;has policy
					$var1 = RegRead ( $path&"\"&$var, $array[2] )
					$cut_path=StringSplit($path,"\")
					;
					if $var1 = "" Then
						$var1 = $no_exist
					EndIf
					;$cut_path[7]="DiskMirrors" or "GroupMirrors"
					IniWrite($output_file,$reg_section_name,"Item"&$temp&"-"&$i,$cut_path[7]&"\"&$var&";"&$array[2]&";"&$var1)
				next

				if $i=1 Then;policy is empty
					$cut_path=StringSplit($path,"\")
					;_ArrayDisplay($cut_path, "$Array_cut")	;test
					IniWrite($output_file,$reg_section_name,"Item"&$temp,$cut_path[7]&"\"&$array[2]&";"&$no_exist)
				EndIf

			EndIf
	Next

	_log("get_reg_value() OUT")

EndFunc ;get_reg_value()

; #FUNCTION# ====================================================================================================================
; Name...........: get_file_status($file_section_name,$output_file)
; Description ...:
; Parameters ....: $file_sectcion_name - resction name to read file
;                  $output_file - output result to this ini file
; Return values .: Success - Index of last added item
;                  Failure - -1, sets @error
; Author ........: Allen Wang

; =======================================================================================================================
Func get_file_status($file_section_name,$output_file)

	 log("get_file_status() IN")

	 Local $temp,$status,$path,$parse,$file_path
	 Local $config_ini_index = IniReadSection($config_filename, $file_section_name)
	 ;_ArrayDisplay($config_ini_index, "$Array")

	 for $temp = 1 To $config_ini_index[0][0]

		local $array = StringSplit($config_ini_index[$temp][1],";")
		;special case:$SYSTEMDIR\drivers;dsksafe.sys

		If Not 0 = StringInStr($array[1], "\") Then
			Local $temp_array = StringSplit($array[1],"\")
			;delete "$"
			$temp_array[1] = StringReplace($temp_array[1], "$", "")
			;get physical path
			$path=get_globalvariable_path($temp_array[1])
			$path=$path&"\"&$temp_array[2]
			$file_path = $path&"\"&$array[2]
			$status = FileExists ($file_path)
			;MsgBox(0,"service status ",$status)
		Else
			$array[1] = StringReplace($array[1], "$", "") ;delete "$"
			;MsgBox(0,"folder name ",$array[1])
			$path=get_globalvariable_path($array[1])
			;MsgBox(0,"path ",$path)
			$file_path = $path&"\"&$array[2]
			$status = FileExists ($file_path)
			;MsgBox(0,"service status ",$status)
		EndIf

		log("get_file_status() MID")

		Switch $status
			Case "1"
					Local $version = FileGetVersion($file_path)
					;MsgBox(0,"version ",$version)
					If "0.0.0.0"=FileGetVersion($file_path) Then
						$version = "0"
					EndIf
					IniWrite($output_file,$file_section_name,"Item"&$temp,$version)
			case "0"
					IniWrite($output_file,$file_section_name,"Item"&$temp,$no_exist)
		EndSwitch
	 Next

	log("get_file_status() OUT")


EndFunc ;get_file_status()

; #FUNCTION# ====================================================================================================================
; Name...........: get_folder_status($folder_section_name,$output_file)
; Description ...:
; Parameters ....: $folder_section_name - section name to read folder
;                  $output_file - output result to this ini file
; Return values .: Success - Index of last added item
;                  Failure - -1, sets @error
; Author ........: Allen Wang

; =======================================================================================================================
Func get_folder_status($folder_section_name,$output_file)

	 ;MsgBox(0,"$folder_section_name = ",$folder_section_name)
	 ;MsgBox(0,"$output_file = ",$output_file)


	 Local $temp,$status,$path,$parse
	 Local $config_ini_index = IniReadSection($config_filename, $folder_section_name)

	 _log("get_folder_status IN")

	 for $temp = 1 To $config_ini_index[0][0]

		$parse = StringReplace($config_ini_index[$temp][1], "$", "") ;delete "$"

		;MsgBox(0,"111 folder name ",$parse)
		$path=get_globalvariable_path($parse)
		;MsgBox(0,"222 path ",$path)
		$status = FileExists ($path)
		;MsgBox(0,"333 service status ",$status)


		;_log("get_folder_status MID")

		Switch $status
			Case "1"
					IniWrite($output_file,$folder_section_name,"Item"&$temp,$status)
			case "0"
					IniWrite($output_file,$folder_section_name,"Item"&$temp,$no_exist)
		EndSwitch
	 Next

	 _log("get_folder_status OUT")

EndFunc ;get_folder_status()

; #FUNCTION# ====================================================================================================================
; Name...........: get_service_status($service_section_name,$output_file)
; Description ...:
; Parameters ....: $folder_section_name - section name to read folder
;                  $output_file - output result to this ini file
; Return values .: Success - Index of last added item
;                  Failure - -1, sets @error
; Author ........: Allen Wang

; =======================================================================================================================
func get_server_status($service_section_name,$output_file)

	 _log("get_server_status() IN")

	 Local $temp,$status
	 Local $config_ini_index = IniReadSection($config_filename, $service_section_name)

	 ;_log("get_server_status() MID $config_filename = " & $config_filename)

	 ;_log("get_server_status() MID $service_section_name = " & $service_section_name)

	 ;_ArrayDisplay($config_ini_index, "$Array")
	 for $temp = 1 To $config_ini_index[0][0]

		  ;_log("get_server_status() MID Enter for loop")


		;MsgBox(0,"service name ",$config_ini_index[$temp][1])
		$status = _ServiceStatus($config_ini_index[$temp][1])
		;_log("$config_ini_index[$temp][1] = " & $config_ini_index[$temp][1])

		;_log("$status = " & $status)

		;MsgBox(0,"service status ",$status)

		;_log("get_server_status() MID")

		Switch $status
			Case "Running"
					IniWrite($output_file,$service_section_name,"Item"&$temp,$success)
			Case "Stopped"
					IniWrite($output_file,$service_section_name,"Item"&$temp,$no_exist)
			case "0"
					IniWrite($output_file,$service_section_name,"Item"&$temp,$no_exist)
		EndSwitch
	 Next

	 _log("get_server_status() OUT")

EndFunc ;get_server_status()


; #FUNCTION# ====================================================================================================================
; Name...........: transfor_parameter_to_path($parameter)
; Description ...: transfor the congfigure item to real path
; Return values .: Success -
;                  Failure - -1, sets @error
; Author ........: Allen Wang

; =======================================================================================================================
Func transfor_parameter_to_path($parameter)


	_log("transfor_parameter_to_path() IN")

	$iIndex = _ArraySearch($glob_ini_index_env_var, $parameter, 0, 0, 0, 1)
	Local $r=""


	_log("transfor_parameter_to_path() MID1")

	If @error Then
		;MsgBox(0, "Not Found", $parameter, 'was not found in the array.')
		_log("transfor_parameter_to_path(): " & $parameter & " was not found in the array.(Error)")
	Else

    	$result = StringRegExp($glob_ini_index_env_var[$iIndex][1], "@")
		_log("transfor_parameter_to_path() MID2")

		If $result = 1 Then

			_log("transfor_parameter_to_path() MID3-1")

			;MsgBox(0, "Search result:", "PASS")
			Switch $glob_ini_index_env_var[$iIndex][1]
				case "@ProgramFilesDir"
					 $glob_ini_index_env_var[$iIndex][1] = $PROGRAMFILESDIR
			    case "@AppDataCommonDir"
					 $glob_ini_index_env_var[$iIndex][1] = $DOCUMENTDIR
			    case "@SystemDir"
					 $glob_ini_index_env_var[$iIndex][1] = $SYSTEMDIR
			EndSwitch
		Else

			_log("transfor_parameter_to_path() MID3-2")

			 ;MsgBox(0, "item:", $glob_ini_index[$iIndex][1])
			 Local $temp = StringRegExp($glob_ini_index_env_var[$iIndex][1], "[$]")
			 If $temp = 1 Then
				;MsgBox(0, "Search result:", "PASS")
				Local $temp1 = StringSplit($glob_ini_index_env_var[$iIndex][1],"\")
				;_ArrayDisplay($temp1, "Split Array")

				For $i=1 To $temp1[0]
					$temp1[$i]=StringReplace($temp1[$i], "$", "")
					;MsgBox(0, "name:",$temp1[$i])

					;MsgBox(0,"Before",$temp1[$i])

					If $temp1[$i] = $falcon_build Then

						$temp_index	= _ArraySearch($glob_ini_index, $temp1[$i], 0, 0, 1, 0)
						$r = $r & $glob_ini_index[$temp_index][1]

					Else
						$temp_index = _ArraySearch($glob_ini_index_env_var, $temp1[$i], 0, 0, 1, 0)

						If @error Then
							$r = $r & "\" & $temp1[$i]
						else
							$r = $r & $glob_ini_index_env_var[$temp_index][1]
						EndIf

					EndIf

				Next

				$glob_ini_index_env_var[$iIndex][1]=$r
				;MsgBox(0, "path$$:",$glob_ini_index[$iIndex][1])
                $r=""	;reset $r

			 EndIf

		EndIf

	EndIf

	_log("transfor_parameter_to_path() OUT")


EndFunc

; #FUNCTION# ====================================================================================================================
; Name...........: get_globalvariable_path($parameter)
; Description ...: get real path by item name,this function should run after transfor_parameter_to_path($parameter)
; Return values .: Success -
;                  Failure - -1, sets @error
; Author ........: Allen Wang

; =======================================================================================================================




Func get_globalvariable_path($parameter)


	$iIndex = _ArraySearch($glob_ini_index_env_var, $parameter, 0, 0, 0, 1)

	If @error Then
		MsgBox(0, "Not Found", $parameter, 'was not found in the array.')
	Else

	;MsgBox(0, "Found", 'PROGRAMFILESDIR was found in the array at position ' & $iIndex & ".")
	;MsgBox(0, "Found", "PROGRAMFILESDIR's value was found in the array at position " & $glob_ini_index[$iIndex][1] & ".")

	return $glob_ini_index_env_var[$iIndex][1]
	EndIf
EndFunc



Func get_action()
	$var = IniRead($globalvariable_file_name, $BuildInfo, $action_key, "---")
    Return $var
EndFunc

Func set_action($var)
	$error = IniWrite($globalvariable_file_name, $BuildInfo, $action_key, $var)
    Return $error
EndFunc


Func get_falcon_build()


	$var = IniRead($globalvariable_file_name, $BuildInfo, $falcon_build, "---")

	;_log("  get_falcon_build $globalvariable_file_name = " & $globalvariable_file_name)
	;_log("  get_falcon_build $falcon_build = " & $falcon_build)
	;_log("  get_falcon_build $var = " & $var)

	Return $var
EndFunc

Func set_falcon_build()

EndFunc

Func get_disksafe_version()
	$var = IniRead($globalvariable_file_name, $BuildInfo, $Disksafe_version, "---")
    Return $var
EndFunc

Func set_disksafe_version()

EndFunc

Func get_disksafe_build()
	$var = IniRead($globalvariable_file_name, $BuildInfo, $DiskSafe_Build, "---")
    Return $var
EndFunc

Func set_disksafe_build()

EndFunc

Func get_ima_version()
	$var = IniRead($globalvariable_file_name, $BuildInfo, $IMA_version, "---")
    Return $var
EndFunc

Func set_ima_version()

EndFunc

Func get_ima_build()
	$var = IniRead($globalvariable_file_name, $BuildInfo, $IMA_build, "---")
    Return $var
EndFunc

Func set_ima_build()

EndFunc

Func get_configure_filename()
	$var = IniRead($globalvariable_file_name, $ConfigPara, "Config_File_Name", "---")
    Return $var
EndFunc

